package Collection;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * @author wuhan
 * @date 2019/9/25 19:20
 * 去重
 */
class Person{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public boolean equals(Object obj) {
        if (! (obj instanceof Person))
            return false;
        Person p=(Person)obj;
        return this.name.equals(p.name)&&this.age==p.age;
    }
    public int hashCode(){
        return  name.hashCode()+age*39;
    }
}





public class ArrayListPractice {
    public static ArrayList singleElement(ArrayList al){
        ArrayList newAL =new ArrayList();
        Iterator iterator = al.iterator();
        while (iterator.hasNext()){
            Object o = iterator.next();
            if (!newAL.contains(o))
                newAL.add(o);
        }
        return newAL;
    }


    public static void main(String[] args) {
        ArrayList arrayList=new ArrayList();
//        arrayList.add("01");        arrayList.add("02");
//        arrayList.add("01");
//        arrayList.add("03");        arrayList.add("03");
//
//        ArrayList arrayList1 = singleElement(arrayList);
//        System.out.println(arrayList1);
        arrayList.add(new Person("wuhan",  100));
        arrayList.add(new Person("wuhn",  100));
        arrayList.add(new Person("wuhan",  100));
        arrayList.add(new Person("wuhan",  100));

//        ListIterator listIterator = arrayList.listIterator();
//        while (listIterator.hasNext()){
//            Person p= (Person) listIterator.next();
//            System.out.println(p.getName());
//        }

        arrayList = singleElement(arrayList);
        for (Object o : arrayList) {
            Person person= (Person) o;
            System.out.println(person.getName()+person.getAge());
        }


    }
}
